package com.yoomiito.app.ui.anyview.goodsinfo.bottom;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.yoomiito.app.base.App;
import com.yoomiito.app.base.BaseActivity;
import com.yoomiito.app.interf.BaseBottomBar;
import com.yoomiito.app.R;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.AllEventID;
import com.yoomiito.app.utils.RouterUtils;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.TouchUtils;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.widget.NoClickLinearLayout;
import com.yoomiito.app.widget.SelectView;

/**
 * 淘宝详情 ---- 底部状态栏
 */
public class TaoBaoBottomBar extends BaseBottomBar {
    private TextView mShareAwardTv;
    private TextView mBuyAwardTv;
    private TextView mShareContentTv;
    private NoClickLinearLayout mCollectionView;
    private int mBuyType;
    private SelectView selectView;

    public TaoBaoBottomBar(Context context, boolean isLogin) {
        super(context, isLogin);
        LayoutInflater.from(context).inflate(R.layout.item_bottom_btn, this, true);
        initView();
    }

    private void initView() {
        selectView = findViewById(R.id.item_bottom_collection);
        mShareContentTv = findViewById(R.id.item_bottom_btn_tv_1);
        mShareAwardTv = findViewById(R.id.item_bottom_btn_fanyong);
        mBuyAwardTv = findViewById(R.id.item_bottom_btn_lingQuan);
        mCollectionView = findViewById(R.id.item_goods_collection);
        mCollectionView.setOnClickListener(v -> {
            if (checkClick()) {
                selectView.setSelect(!selectView.isSelected());
                TouchUtils.onCollectEvent(AllEventID.TaoBaoCollectionEventId, selectView.isSelected() ? 1 : 0);
                dispatchEvent(selectView.isSelected(), false, false);
            }
        });
//        selectView.setNoSelect(mLogin);
//        selectView.setClickListener(v -> {
//            if (checkClick()){
//                TouchUtils.onCollectEvent(AllEventID.TaoBaoCollectionEventId, selectView.isSelected()?1:0);
//                dispatchEvent(selectView.isSelected(),false,false);
//
//            }
//        });

        findViewById(R.id.item_bottom_btn_left).setOnClickListener(v -> {
            if (checkClick()) {
                TouchUtils.onEvent(AllEventID.TaoBaoShareEvnetId, UserInfoConfig.getRoleId(), UserInfoConfig.getAgent());
                RouterUtils.toShareGoods((BaseActivity) mContext, String.valueOf(mGoodsDetail.getNum_iid()), AllCode.TaoBao);
            }
        });

        findViewById(R.id.item_bottom_btn_right).setOnClickListener(v -> {
            if (checkClick()) {
                TouchUtils.onEvent(AllEventID.TaoBaoBuyEventId, UserInfoConfig.getRoleId(), UserInfoConfig.getAgent());
                if (mBuyType == 1) {//立即购买
                    RouterUtils.TaoBaoWithUrl(mGoodsDetail.getItem_url());
                } else {
                    RouterUtils.TaoBaoWithUrl( mGoodsDetail.getCoupon_click_url());
                }
            }
        });

    }

    @Override
    public void updateInfo(GoodsDetail detail) {
        super.updateInfo(detail);
        if (SpannableUtils.isEmptyOr0(detail.getCoupon_price())) {//任何商品  只要没有优惠券 就是立即购买   1：立即购买    0：领券购买
            mBuyAwardTv.setText("立即购买");
            mBuyType = 1;
        }
        if (App.mAppConfig.getIsLogin()) {
//            if (UserInfoConfig.getRoleId() >= LevelType.VIP_4) {
            if (!SpannableUtils.isEmptyOr0(detail.getGet_money())) {

                mBuyAwardTv.setText("返佣：¥" + detail.getGet_money());
                mShareAwardTv.setText("返佣：¥" + detail.getGet_money());
                mBuyAwardTv.setVisibility(View.VISIBLE);
                mShareAwardTv.setVisibility(View.VISIBLE);
//            } else {
//                mShareContentTv.setText("分享");
//                mBuyAwardTv.setVisibility(View.GONE);
//                mShareAwardTv.setVisibility(View.GONE);
            }
        }
        selectView.setSelect(detail.getIs_collection() == 1 ? true : false);
    }
}
